package top.hudk.sql.generator;

import com.yueyue.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import top.hudk.sql.strategy.DefaultHashShardingStrategy;
import top.hudk.sql.strategy.abstracts.HashShardingStrategy;
import top.hudk.sql.domin.ShardingResult;
import top.hudk.sql.domin.DriverReportRecordDataMate;

import java.util.ArrayList;
import java.util.List;

/**
 * 请输入描述信息
 *ShardingStrategy
 * @author HuDongkang
 * @date 2022/9/1 10:37
 */
@Slf4j
public class DriverReportRecordSqlGenerator extends AbstractSqlGenerator<DriverReportRecordDataMate, HashShardingStrategy, String> {

    private static final String SQL_TEMPLATE =
            "update `unicorn_finance_%s`.`unicorn_finance_driver_report_record_%s` set insurance_no = '%s' where relation_order_id = '%s' and insurance_no = '%s';";

    private static final int DB_TOTAL_COUNT = 2;

    private static final int TABLE_TOTAL_COUNT = 8;

    public static void main(String[] args) {
        new DriverReportRecordSqlGenerator().gen();
    }


    @Override
    protected ShardingResult doShardingStrategy(HashShardingStrategy shardingStrategy, String shardingValue) {
        return shardingStrategy.sharding(shardingValue, DB_TOTAL_COUNT, TABLE_TOTAL_COUNT);
    }

    @Override
    protected String format(ShardingResult shardingResult, DriverReportRecordDataMate driverReportRecordDataMate){
        return String.format(SQL_TEMPLATE,
                shardingResult.getDbNo(),
                shardingResult.getTableNo(),
                driverReportRecordDataMate.getInsuranceNo(),
                driverReportRecordDataMate.getOrderId(),
                driverReportRecordDataMate.getInsuranceNoErr()
        );
    }

    @Override
    protected DefaultHashShardingStrategy getShardingStrategy() {
        return new DefaultHashShardingStrategy();
    }

    @Override
    protected List<DriverReportRecordDataMate> getDataMateList(){
        final String dataMateJsonStr =
                "[{\"orderId\":\"2209028989001171866\",\"driverId\":\"4615291431232\"," +
                        "\"insuranceNoErr\":\"661520202212996602496605\",\"insuranceNo\":\"668701202212996602136764\"}]";
        return new ArrayList<>(JsonUtils.fromListValue(dataMateJsonStr, DriverReportRecordDataMate.class));
    }
}
